package cn.it.shop.action;

import java.util.Map;

import org.apache.struts2.interceptor.ParameterAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.it.shop.pojo.Forder;
import cn.it.shop.pojo.SendData;
import cn.it.shop.pojo.User;

@Controller
@Scope("prototype")
public class PayAction extends BaseAction<SendData> implements ParameterAware {
	
	private Map<String, String[]> parameters;

	public String pay$(){
		// 1:获取支付参数,商品编号和支付金额要从session中获取
		Forder forder=(Forder)session.get("oldForder");
		// 存储用户的邮件, 和手机号码,支付成功的时候需要, service中的sendData.setPa_MP(""); 需要取消
		User user=(User)session.get("user");
		model.setPa_MP(user.getEmail() + "|" + user.getPhone());
		model.setP2_Order(forder.getId().toString());
		model.setP3_Amt(forder.getTotal().toString());
		payService.saveDataToRequest(request, model);
		return "reqpay";
	}
	
	public void success(){
		boolean isOK=payService.checkBackData(parameters);
		// 如果要测试,换成申请的域名
		if(isOK){
			// 存储支付成功的订单ID
			int id=Integer.parseInt(parameters.get("r6_Order")[0].toString());
			// 获取发送的邮箱地址
			String r8_MP = parameters.get("r8_MP")[0].toString();
			System.out.println("发送的信息:" + r8_MP);
			String email=r8_MP.split("\\|")[0];
			String number=r8_MP.split("\\|")[1];
			System.out.println("----支付成功----");
			System.out.println("发送地址为:" + email + "手机号码为:" + number + "订单编号：" + id);
			// 1: 修改订单状态
			forderService.update(id, 2);
			// 3: 发送短信信息
			sendMessageUtil.sendMessage(number, id);
			// 2: 发送邮件信息, 不能从session中去获取任何,防止session已经失效,需要的信息通过 pa_MP传出,通过 r8_MP返回
			emailUtil.sendMessage(id, email);
		}else{
			System.out.println("----支付失败----");
		}
	}

	@Override
	public void setParameters(Map<String, String[]> parameters) {
		// TODO Auto-generated method stub
		this.parameters=parameters;
	}
}
